﻿<%@ WebHandler Language="C#" Class="AriticleHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.SessionState;
using System.Threading;

public class AriticleHandler : IHttpHandler,IRequiresSessionState {
    
    public void ProcessRequest (HttpContext context) {
        Thread.Sleep(400);
        switch (context.Request["act"].ToLower())
        {
            case "add":
                AddPost(context);
                break; 
            case "edit":
                EditPost(context);
                break;
            case "getclass":
                GetClass(context);
                break;
            case "gettag":
                GetTag(context);
                break;
            case "getpost":
                GetPost(context);
                break;
            default:
                break;
        }
        
        context.Response.ContentType = "text/plain";
        context.Response.Write("未知动作");
    }

    public void AddPost(HttpContext context)
    {
        Cstudy.MODEL.artcles ariticle = new Cstudy.MODEL.artcles();
        ariticle.AuthorID = (int)context.Session["userid"];
        ariticle.ClassID = Convert.ToInt32(context.Request.Params["cid"]);
        ariticle.Content = context.Request.Params["content"];
        ariticle.States = (Cstudy.MODEL.artcles.ArtcleStates)(Convert.ToInt32(context.Request.Params["state"]));
        ariticle.TagID = Convert.ToInt32(context.Request.Params["tid"]);
        ariticle.Title = context.Request.Params["title"];
        if (ariticle.Content.Length > 50)
        {
            ariticle.Summary = ariticle.Content.Substring(0, 50);
        }
        else
        {
            ariticle.Summary = ariticle.Content;
        }

        bool result = new Cstudy.BLL.Artcles().AddArtcle(ariticle);
        context.Response.ContentType = "json/application";
        context.Response.Write("{\"result\":\"" + result.ToString() + "\"}");
        context.Response.End();
    }

    private void EditPost(HttpContext context)
    {
        Cstudy.MODEL.artcles ariticle = new Cstudy.MODEL.artcles();
        ariticle.postID = Convert.ToInt32(context.Request.Params["pid"]);
        ariticle.AuthorID = (int)context.Session["userid"];
        ariticle.ClassID = Convert.ToInt32(context.Request.Params["cid"]);
        ariticle.Content = context.Request.Params["content"];
        if (ariticle.Content.Length > 50)
        {
            ariticle.Summary = ariticle.Content.Substring(0, 50);
        }
        else
        {
            ariticle.Summary = ariticle.Content;
        }
        ariticle.States = (Cstudy.MODEL.artcles.ArtcleStates)(Convert.ToInt32(context.Request.Params["state"]));
        ariticle.TagID = Convert.ToInt32(context.Request.Params["tid"]);
        ariticle.Title = context.Request.Params["title"];
        ariticle.EditDate = DateTime.Now;

        bool result = new Cstudy.BLL.Artcles().EditArtcle(ariticle);
        context.Response.ContentType = "json/application";
        context.Response.Write("{\"result\":\"" + result.ToString() + "\"}");
        context.Response.End();
    }

    private void GetClass(HttpContext context)
    {
        System.Data.DataSet ds = new Cstudy.BLL.postClass().SelectPostClassOrderBy();
        IList<Cstudy.MODEL.postclass> classes = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.postclass>(ds.Tables[0]);
        string json = Cstudy.Tools.JsonHelper.ToJson(classes);
        context.Response.ContentType = "json/application";
        context.Response.Write(json);
        context.Response.End();
    }

    private void GetTag(HttpContext context)
    {
        System.Data.DataSet ds =  new Cstudy.BLL.postTag().SelectPostTag();
        IList<Cstudy.MODEL.postTag> tags = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.postTag>(ds.Tables[0]);
        string json = Cstudy.Tools.JsonHelper.ToJson(tags);
        context.Response.ContentType = "json/application";
        context.Response.Write(json);
        context.Response.End();
    }

    private void GetPost(HttpContext context)
    {
        int pid = Convert.ToInt32(context.Request.Params["pid"]);
        System.Data.DataSet ds = new Cstudy.BLL.Artcles().SelectArtcleByID(pid, false);
        Cstudy.MODEL.artcles post = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.artcles>(ds.Tables[0].Rows[0]);
        string json = Cstudy.Tools.JsonHelper.ToJson(post);
        context.Response.ContentType = "json/application";
        context.Response.Write(json);
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}